package top.software.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import top.software.service.FileService;
import top.software.service.UserService;
import top.software.util.Message;
import top.software.util.MessageUtil;

/**
 * @ClassName: UserController
 * @Author: Amosen
 * @Since: 2021/4/20
 */

// http://localhost:8080/back/user/login

@Controller
@RequestMapping("/user")
@ResponseBody
public class UserController {

	@Autowired
	private UserService userService;

	@Autowired
	private FileService fileService;

	// 检查某个字符串是否为空
	private boolean strCanUse(String str) {
		if (str == null) {
			return false;
		}
		String newStr = str.trim();
		return newStr.length() != 0;
	}

	// 登录
	@RequestMapping("/login")
	public Message login(@RequestParam("account") String account, @RequestParam("password") String password) {
		if (strCanUse(account) && strCanUse(password)) {
			return userService.login(account, password);
		} else {
			return MessageUtil.fail();
		}
	}

	// 注册
	@RequestMapping("/register")
	public Message register(@RequestParam("account") String account, @RequestParam("password") String password) {
		if (strCanUse(account) && strCanUse(password)) {
			return userService.register(account, password);
		} else {
			return MessageUtil.fail();
		}
	}

	// 修改密码
	@RequestMapping("/update/password")
	public Message updatePassword(@RequestParam("account") String account,
			@RequestParam("newPassword") String newPassword) {
		if (strCanUse(account) && strCanUse(newPassword)) {
			return userService.updatePassword(account, newPassword);
		} else {
			return MessageUtil.fail();
		}
	}

	// 权限变更
	@RequestMapping("/update/qualification")
	public Message modifyQualification(@RequestParam("loginAccount") String loginAccount,
			@RequestParam("modifyAccount") String modifyAccount, @RequestParam("newAuth") Integer auth) {
		if (strCanUse(loginAccount) && strCanUse(modifyAccount) && auth != null) {
			return userService.modifyQualification(loginAccount, modifyAccount, auth);
		} else {
			return MessageUtil.fail();
		}
	}

	// 获取成员
	@RequestMapping("/get/all")
	public Message getAllUser() {
		return userService.getAllUser();
	}

	@RequestMapping("/search")
	public Message search(@RequestParam("param") String param) {
		Message userVague = userService.getUserVague(param, null);
		Message userVague1 = userService.getUserVague(null, param);
		Map<String, Object> data = userVague.getData();
		for (Map.Entry<String, Object> entry : data.entrySet()) {
			userVague1.addData(entry.getKey(), entry.getValue());
		}
		return userVague1;
	}

	@RequestMapping("/screen/qualification")
	public Message screenQualification(@RequestParam("auth") Integer auth) {
		return userService.screenQualification(auth);
	}

	@RequestMapping("/add")
	public Message addUser(@RequestParam("loginAccount") String loginAccount, @RequestParam("account") String account,
			@RequestParam("auth") Integer auth) {
		if (strCanUse(loginAccount) && strCanUse(account)) {
			return userService.addUser(loginAccount, account, auth);
		} else {
			return MessageUtil.fail();
		}
	}

	@RequestMapping("/delete")
	public Message deleteUser(@RequestParam("loginAccount") String loginAccount,
			@RequestParam("account") String account) {
		if (strCanUse(loginAccount) && strCanUse(account)) {
			return userService.deleteUser(loginAccount, account);
		} else {
			return MessageUtil.fail();
		}
	}

	@PostMapping("/upload")
	public Message uploadFile(@RequestParam("uploader") String uploader,
			@RequestParam("downloadAuth") Integer downloadAuth, @RequestParam("file") MultipartFile[] files,
			HttpServletRequest req) {
		if (strCanUse(uploader)) {
			return fileService.uploadFile(req, uploader, downloadAuth, files);
		} else {
			return MessageUtil.fail();
		}
	}

	@RequestMapping("/download")
	public Message downloadFile(@RequestParam("loginAccount") String loginAccount,
			@RequestParam("fileId") Integer fileId, HttpServletResponse response) {
		return fileService.downloadFile(loginAccount, fileId, response);
	}

	@RequestMapping("/get/filelist")
	public Message getFileList() {
		return fileService.getAllFile();
	}

}
